using System;
using System.Collections.Generic;
using System.ComponentModel;

using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CodePlex.SpaceBlock;
using CodePlex.SpaceBlock.FS;
using CodePlex.Resourceful;


namespace CodePlex.SpaceBlock.UI.Controls.Tools
{
    public partial class ChangePasswordForm : CodePlex.SpaceBlock.UI.Controls.BaseForm
    {

        public static string Show(Form parent, bool promptForCurrentPassword, Operation cancel, Fn<string,string,bool> ok)
        {
            ChangePasswordForm form = new ChangePasswordForm();
            form.Icon = UIManager.Instance.GetIcon(UIConstants.ImageHints.Key);
            form.Text = Properties.Messages.ChangePassword_Title;
            form.Initialize( promptForCurrentPassword, cancel, ok);
            form.StartPosition = FormStartPosition.CenterScreen;
            DialogResult result = form.ShowDialog(parent);

            if (result == DialogResult.Cancel)
            {
                return null;
            }
            return form.NewPassword;
        }



        private Operation _cancelOperation;
        private Fn<string,string,bool> _okOperation;
        private bool _preventFormClosing;

        public ChangePasswordForm()
        {
            InitializeComponent();

            this.FormClosing += delegate(object sender, FormClosingEventArgs e)
            {
                if (_preventFormClosing)
                {
                    e.Cancel = true;
                    _preventFormClosing = false;
                }
            };

           
            EventHandler checkSame = delegate 
            {
                _saveButton.Enabled = 
                    _newPasswordBox.Text == _newPasswordBox2.Text 
                    && _newPasswordBox2.Text.Length >= 3;
            };
            _newPasswordBox.TextChanged += checkSame;
            _newPasswordBox2.TextChanged += checkSame;

        }

        public string NewPassword
        {
            get
            {
                return _newPasswordBox2.Text;
            }
        }

        public string OldPassword
        {
            get
            {
                if (!_currentPasswordBox.Visible)
                {
                    return null;
                }
                return _currentPasswordBox.Text;
            }
        }










        private void Initialize( bool promptForCurrentPassword, Operation cancel, Fn<string,string,bool> ok)
        {
            _cancelOperation = cancel;
            _okOperation = ok;

            _saveButton.Enabled = false;
            _currentPasswordBox.Visible = promptForCurrentPassword;
            _currentPasswordLabel.Visible = promptForCurrentPassword;


            string description = promptForCurrentPassword ?
                Properties.Messages.ChangePassword_Reset :
                Properties.Messages.ChangePassword_New;


            _descriptionLabel.Text = description;
        }

        private void _cancelButton_Click(object sender, EventArgs e)
        {
            _cancelOperation();
        }

        private void _saveButton_Click(object sender, EventArgs e)
        {
            bool rt = _okOperation(this.OldPassword,this.NewPassword);
           
            if (!rt)
            {
                _preventFormClosing = true;
            }
        }


    
     
    }
}

